package com.android.sunnyweather.logic.dao

import android.content.Context
import androidx.core.content.edit
import com.android.sunnyweather.app.MyApplication
import com.android.sunnyweather.logic.model.Place
import com.google.gson.Gson

object PlaceDao {

    private fun sharePreferences() = MyApplication.myApp.getSharedPreferences("sunny_weather",Context.MODE_PRIVATE)

    fun savePlace(place: Place){
        sharePreferences().edit{
            putString("place",Gson().toJson(place))
        }
    }

    fun getSavedPlace(): Place{
        val placeJson = sharePreferences().getString("place","")
        return Gson().fromJson(placeJson,Place::class.java)
    }

    fun isPlaceSaved() = sharePreferences().contains("place")

}